package cn.xiaopengstack.types.model;

import cn.xiaopengstack.types.enums.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author jiangyangang
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Response<T> implements Serializable {

    private String code;
    private String info;
    private T data;


    public static <T> Response<T> success(T data){
        Response<T> response = new Response<>();
        response.setCode(ResponseCode.SUCCESS.getCode());
        response.setInfo(ResponseCode.SUCCESS.getInfo());
        response.setData(data);
        return response;
    }

    public static <T> Response<T> error(T data){
        Response<T> response = new Response<>();
        response.setCode(ResponseCode.UN_ERROR.getCode());
        response.setInfo(ResponseCode.UN_ERROR.getInfo());
        response.setData(data);
        return response;
    }

    public static <T> Response<T> error(T data, ResponseCode code){
        Response<T> response = new Response<>();
        response.setCode(code.getCode());
        response.setInfo(code.getInfo());
        response.setData(data);
        return response;
    }
}
